<?php
/**
 *      [Discuz!] (C)2001-2099 Comsenz Inc.
 *      This is NOT a freeware, use is subject to license terms
 *
 *      $Id: mapi_model_article.php 338 2013-04-27 08:06:38Z laoguozhang $
 */

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

/**
 * 系统运行日志
 * 
 */
class dzf_lib_log extends dzf_base_model {
	
	public function __construct() {
		parent::__construct();
	}
	
	public function insert($data) {
		return;
		if($data) {
			$data = array_merge(array(
				'uid' => DZF::app()->member['uid'],
				'dateline' => TIMESTAMP,
			), $data);
			DZF::t('common_log')->insert($data);
		}
	}
	public function slow_request($uri, $runtime) {
		return;
		$this->insert(array(
			'type' => '1',
			'data' => $uri,
			'runtime' => $runtime * 1000000,
		));
	}
	
	public function slow_sql($sql, $runtime) {
		return;
		$this->insert(array(
			'type' => '2',
			'data' => $sql,
			'runtime' => $runtime * 1000000,
		));
	}

	/**
	 * 系统运行日志
	 * @param int $type
	 * type: 同步更新 function_core.php中的dlog();
		 * 1 慢请求链接
		 * 2 慢SQL
		 * 3 youtu
		 * 4 apd
		 * 5 system
		 * 6 exception
		 * 7 cmem
		 * 8 has null or Array
		 * 9 L5
		 * 10 dc
	 * @param sting $data 
	 * @param int $runtime
	 */
	public function dlog($type, $data, $runtime) {
		return;
		if(mt_rand(0, 100) !== 0) {
			return false;
		}
		$uri = $ip = '';
		if($obj = DZF::app()) {
			$uri = $obj->request()->getRequestUri();
			$ip = $obj->ip;
		} else {
			$uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'] .'?'. $_SERVER['QUERY_STRING'];
		}
		$insertdata = array('msg' => $data, 'server' => DZF::lib('ip')->localip(), 'trace' => self::debug_backtrace(), 'uri' => $uri, 'ip' => $ip);//记录详细信息
		$insert = array(
			'type' => $type,
			'uid' => DZF::app()->member['uid'],
			'runtime' => $runtime,
			'data' => json_encode($insertdata),
			'dateline' => TIMESTAMP,
		);
		DZF::t('common_log')->insert($insert);
	}
	
	
	/**
	 * 内部使用的记录日志
	 * @param int $errno
	 * @param string $errstr
	 * @param string $errfile
	 * @param int $errline
	 */
	public static function debug_backtrace() {
		
		$data['trace'] = array();
		$debug_backtrace = debug_backtrace();
		$debug_backtrace = array_slice($debug_backtrace, 1, count($debug_backtrace) - 3);
		krsort($debug_backtrace);
		foreach ($debug_backtrace as $k => $error) {
			$trace = array();
			$trace['File'] = str_replace(DISCUZ_ROOT, '', $error['file']);
			$func = isset($error['class']) ? $error['class'] : '';
			$func .= isset($error['type']) ? $error['type'] : '';
			$func .= isset($error['function']) ? $error['function'] : '';
			$trace['Func'] = $func;
			$trace['Line'] = sprintf('%04d', $error['line']);
			$data['trace'][] = $trace;
		}

		$data = str_replace(array("\t", "\r", "\n"), " ", $data);
//		$message = json_encode($data);
//		$hash = md5($message);
//		$data['hash'] = $hash;
		return $data;
	}
}
